package com.yyge.controller;

import cn.hutool.crypto.digest.BCrypt;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yyge.context.JwtContext;
import com.yyge.context.RedisContext;
import com.yyge.dto.LoginDto;
import com.yyge.dto.R;
import com.yyge.dto.ResultCode;

import com.yyge.entity._____;
import com.yyge.service._____Service;

import com.yyge.util.TokenUtil;
import com.yyge.vo.LoginUserVo;
import com.yyge.vo.LoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Api("登陆")
@RestController
public class LoginController {
    @Autowired
    private RedisContext redisContext;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private _____Service service;

    @Autowired
    private TokenUtil tokenUtil;

    @ApiOperation("登陆请求")
    @PostMapping("login")
    public R login(@Valid @RequestBody @ApiParam("登陆表单模型") LoginDto loginDto){

        //System.out.println(loginDto);
        //根据UUID获取验证码
        String uuid=loginDto.getUuid();

        //获取redis中验证码的key
        String key=redisContext.getCaptchaKey(uuid);


        //到redis中获取验证码
        Object value=redisTemplate.opsForValue().get(key);
        if(value == null){
            //验证码失效
            return R.error(ResultCode.SYSTEM_CODE_INVALID);
        }

         //消费验证码(从redis中删除)
        redisTemplate.delete(key);

        //获取验证码
        String code=value.toString();

        if (!code.equalsIgnoreCase(loginDto.getCode())){
            //验证码失效
            return R.error(ResultCode.SYSTEM_CODE_ERROR);
        }
        _____ __.MinName.__=new _____();
        __.MinName.__.set__.LoginName.__(loginDto.getLoginName());
        QueryWrapper wrapper = new QueryWrapper(__.MinName.__);
        __.MinName.__ = service.getOne(wrapper);

        if(__.MinName.__ == null){
            //用户不存在
            return R.error(ResultCode.ACCOUNT_NOT_EXISTS);
        }

        //验证密码
        //if (BCrypt.hashpw(loginDto.getLoginPwd()).equals(loginUser.getLoginPwd())){
       if(!BCrypt.checkpw(loginDto.getLoginPwd(),__.MinName.__.get__.PwdName.__())){
        // System.out.println(loginDto.getLoginPwd());
        // System.out.println(BCrypt.hashpw(loginDto.getLoginPwd()));
        // System.out.println(loginUser.getLoginPwd());
            //密码错误
            return R.error(ResultCode.ACCOUNT_INVALID_PWD);

        }

        //状态验证
//        if(loginUser.getStatus() != Employee.STATUS_OK){
//            //状态错误
//            return R.error(ResultCode.ACCOUNT_INVALID);
//        }
        <---LoginUser--->
        String token=tokenUtil.createToken(loginUser);
        return R.ok(token);
    }

    @ApiOperation("退出请求")
   @PostMapping("logout")
   public R logout(@RequestHeader("Authorization") @ApiParam("退出验证模型")  String auth){
        tokenUtil.removeRedisToken(auth);
        return R.ok();
   }

    @ApiOperation("获取用户请求")
    @GetMapping("/user/info")
    //@RequestHeader 获取请求头中指定key的值
    public R getInfo(){
        LoginVO vo = tokenUtil.loginVOThreadLocal.get();
        if(vo == null){
            return R.error(ResultCode.SYSTEM_TOKEN_INVALID);
        }
        return R.ok(vo);
    }
}
